package com.bdqn.service.impl;

import com.bdqn.mapper.SysUserMapper;
import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysUserService;
import com.bdqn.util.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * SysUserServiceImpl
 *
 * @author LILIBO
 * @since 2024/7/30
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    /**
     * 引入数据访问层
     */
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 添加用户
     *
     * @param sysUser
     * @return
     */
    @Override
    public boolean add(SysUser sysUser) {
        if (sysUser == null) {
            return false;
        }
        return sysUserMapper.add(sysUser) == 1;
    }

    /**
     * 用户登录
     *
     * @param account
     * @param password
     * @return 登录用户信息
     */
    @Override
    public SysUser login(String account, String password) {
        if (account == null || password == null) {
            return null;
        }
        return sysUserMapper.getByAccountAndPassword(account, password);
    }

    /**
     * 查询符合条件的总数据量
     *
     * @param queryRealName
     * @param queryRoleId
     * @return
     */
    @Override
    public Long count(String queryRealName, Long queryRoleId) {
        return sysUserMapper.count(queryRealName, queryRoleId);
    }

    /**
     * 根据
     *
     * @param queryRealName
     * @param queryRoleId
     * @param page
     * @return
     */
    @Override
    public List<SysUser> page(String queryRealName, Long queryRoleId, Page page) {
        Long pageSize = page.getPageSize(); // 5
        Long pageIndex = (page.getCurrentPageNo() - 1) * pageSize; // 1 -> 0 ; 2 -> 5
        return sysUserMapper.page(queryRealName, queryRoleId, pageIndex, pageSize);
    }

    /**
     * 判断账号是否存在
     *
     * @param account
     * @return
     */
    @Override
    public boolean exist(String account) {
        if (account == null) {
            return false;
        }
        return sysUserMapper.exist(account) == 1;
    }

    /**
     * 根据ID查询单个用户
     *
     * @param userId
     * @return
     */
    @Override
    public SysUser getById(Long userId) {
        if (userId == null) {
            return null;
        }
        return sysUserMapper.getById(userId);
    }

    /**
     * 根据用户ID删除用户
     *
     * @param uploadPath
     * @param userId
     * @return -1表示不存在，1表示成功，0表示失败
     */
    @Override
    public int del(String uploadPath, Long userId) {
        // 查找用户是否存在（不存在时返回-1）
        SysUser sysUser = this.getById(userId);
        if (sysUser == null) {
            return -1;
        }
        // 先删除用户上传的身份证图片数据
        String idPicPath = sysUser.getIdPicPath();
        if (idPicPath != null) {
            idPicPath = idPicPath.split("/")[3];
            File file = new File(uploadPath + "/" + idPicPath);
            boolean delete = file.delete();
            System.out.println("[" + file.getPath() + "]资源删除" + (delete ? "成功" : "失败"));
        }
        // 先删除用户上传的工作证图片数据
        String workPicPath = sysUser.getWorkPicPath();
        if (workPicPath != null) {
            workPicPath = workPicPath.split("/")[3];
            File file = new File(uploadPath + "/" + workPicPath);
            boolean delete = file.delete();
            System.out.println("[" + file.getPath() + "]资源删除" + (delete ? "成功" : "失败"));
        }

        // 存在则删除
        return sysUserMapper.del(userId);
    }

}
